<template>
  <div>
    <!--    面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    卡片区域-->
    <el-card>
      <el-row :gutter="20">

        <el-col :span="10">
          <el-input
              placeholder="请输入搜索内容"
              v-model="queryInfo.query"
              clearable
              @clear="getGoodList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>

        <!-- 表格区域-->
        <el-table :data="goodList" border stripe>
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column width="95px" label="商品价格(元)" prop="goods_price"></el-table-column>
          <el-table-column width="70px" label="商品重量" prop="goods_weight"></el-table-column>
          <el-table-column width="140px" label="创建时间" prop="add_time">
            <template slot-scope="scope">
              {{ scope.row.add_time | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column width="130px" label="操作" prop="">
            <template slot-scope="scope">
              <!--            修改商品-->
              <el-button type="primary" icon="el-icon-edit" size="mini"
                         @click="showEditDialog(scope.row.goods_id)"></el-button>
              <!--            删除商品-->
              <el-button type="danger" icon="el-icon-delete" size="mini"
                         @click="showDeleteDialog(scope.row.goods_id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--        分页区域-->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </el-row>
    </el-card>

  </div>
</template>

<script>
export default {
  name: "List",
  data() {
    return {
      queryInfo: {
        // 查询的关键字
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 商品列表
      goodList: [],
      total: 0
    }
  },
  created() {
    this.getGoodList()
  },
  methods: {
    // 获取商品列表
    async getGoodList() {
      const {data: res} = await this.$http.get('goods', {params: this.queryInfo})
      console.log('res', res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success('获取商品列表成功')
      this.goodList = res.data.goods
      console.log('this.goodList', this.goodList)
      this.total = res.data.total


    },
    // 展示编辑对话框
    showEditDialog(goodsId){
      this.editDialogVisible = true
      console.log('re',goodsId)
    },
    // 处理每页显示条数的改变
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodList()

    },
    // 处理当前页发生改变的回调
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodList()
    },
    // 根据id删除商品
    async showDeleteDialog(deleteId) {
      console.log(deleteId)
      const confirmResult = await this.$confirm('确认删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消')
      }

     const {data:res} = await this.$http.delete(`goods/${deleteId}`)
      if(res.meta.status !==200){
        return this.$message.error(res.meta.msg)
      }
      this.$message.success('删除成功')
      this.getGoodList()
    },
    // 跳转到添加商品页面
    goAddPage(){
      this.$router.push('/goods/add')
    }

  }
}
</script>

<style lang="less" scoped>

</style>
